<?php

/**
 * 短信模块
 *
 * @author    yanxf <walkfine@gmail.com>
 * @author    lizaifang <lizf@wanthings.com>
 * @copyright 成都网信天成科技有限公司
 * @version    1.1
 */
class SMS
{
    /**
     * 短信发送
     * @param string $content 短信内容
     * @param string $number 短信号码
     * @return array|bool|mixed
     * error int 如果0, 则正常
     * msg string
     */
    public static function send($content, $number)
    {
        if (empty($content)) {
            $result = array('error' => 998, 'msg' => '短信内容为空');
            return $result;
        } else if (empty($number)) {
            $result = array('error' => 997, 'msg' => '手机号码为空');
            return $result;
        }
        return SMS::luosimaoSend($content, $number);

        $url = 'http://sms.daoser.com/sms/send/message/' . urlencode($content) . '/phone/' . $number;
        return SMS::get($url);
    }

    protected static function mkheader()
    {
        $headers = array(
            'Host: erp.wanthings.com',
            'Connection: keep-alive',
            'Cache-Control: no-cache',
            'Pragma: no-cache',
            'User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4',
            'Accept: image/png,image/*;q=0.8,*/*;q=0.5',
            'Referer: http://www.scjj.gov.cn:8635/',
            'Accept-Encoding: gzip,deflate,sdch',
            'Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4',
            'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3',
//          'Cookie: ASP.NET_SessionId=usvzvwrp2qkwo3ejd5miwq45'
        );
        return $headers;
    }

    protected static function get($url, $data = null)
    {
        //  session_start();
        //  $sid = session_id();
        //  $ckfilename = dirname(__FILE__).'/../../assets/cookie_'.$sid;
        //  $headers = SMS::mkheader();
        $ch = curl_init();
        // 2. 设置选项，包括URL
        //  curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
        //  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_URL, $url);

        //  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1'); // 模拟用户使用的浏览器
        //  curl_setopt($ch, CURLOPT_REFERER, 'http://www.scjj.gov.cn:8635/login.aspx'); // referer

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 获取的信息以文件流的形式返回
        //  curl_setopt($ch, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        //  if(!file_exists ($ckfilename))
        //      curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfilename); // 存放Cookie信息的文件名称
        //  curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfilename); // 读取上面所储存的Cookie信息

        if (!empty($data)) {
            curl_setopt($ch, CURLOPT_POST, 1); // 发送一个常规的Post请求
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        }

        // 3. 执行并获取HTML文档内容
        $output = curl_exec($ch);
        // 4. 释放curl句柄
        curl_close($ch);

        //  $fp = fopen(dirname(__FILE__).'/../../assets/log_'.$sid,'ab');
        //  fwrite($fp,$output.'\r\r\r\r\r');
        //  fclose($fp);

        return $output;
    }


    /**
     * @param $message
     * @param string $mobile
     * @param string $signature
     * @return array|mixed
     * error int 如果0, 则正常
     * msg string
     */
    protected static function luosimaoSend($message, $mobile = '18180860426', $signature = '西华记忆')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://sms-api.luosimao.com/v1/send.json");

        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_SSLVERSION, 3);

        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD, 'api:key-5734f82a0d0e8f39323041733f013115');


        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array('mobile' => $mobile, 'message' => $message . '【' . $signature . '】'));

        $res = curl_exec($ch);
        $errno = curl_errno($ch);
        if (!$errno) {
            $result = json_decode($res, true);
        } else {
            $result = array('error' => $errno, 'msg' => curl_error($ch));
        }
        return $result;
    }

    /**
     *
     *
     * @return array|mixed
     * array
     * error int 如果0, 则正常
     * msg str
     * deposit int 剩余短信数量
     */
    public static function luosimaoStatus()
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://sms-api.luosimao.com/v1/status.json");

        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_SSLVERSION, 3);

        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch, CURLOPT_USERPWD, 'api:key-5734f82a0d0e8f39323041733f013115');

        $res = curl_exec($ch);
        $errno = curl_errno($ch);
        if (!$errno) {
            $result = json_decode($res, true);
        } else {
            $result = array('error' => $errno, 'msg' => curl_error($ch));
        }
        return $result;
    }
}